package com.panshi.util;

import com.panshi.entity.AppEssayT;
import com.panshi.entity.AppMatterT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
public class QueryUtils {

    @Autowired
    private ZxUtils zxUtils;

    @Autowired
    private QaUtils qaUtils;

    public List queryLimit(String type, int page, int limit) {
        if (limit < 0) {
            limit = 5;
        }
        List arrayList = new ArrayList();
        if (type.equals("10")) {  //推荐
            //初始化咨询
            List<AppEssayT> appEssayTS = zxUtils.zxList(page, limit, "-1");
            List<AppMatterT> appMatterTS = qaUtils.qaList(0, page, limit, "-1");


            arrayList.addAll(appEssayTS);
            arrayList.addAll(appMatterTS);
            Collections.shuffle(arrayList);

        } else if (type.equals("15")) { //热度read_count
            //问答列表
            //初始化咨询
            List<AppEssayT> appEssayTS = zxUtils.zxList(page, limit, "read_count");
            List<AppMatterT> appMatterTS = qaUtils.qaList(0, page, limit, "read_count");
            arrayList.addAll(appEssayTS);
            arrayList.addAll(appMatterTS);
            Collections.shuffle(arrayList);
        } else if (type.equals("20")) {//最新
            //初始化咨询
            List<AppEssayT> appEssayTS = zxUtils.zxList(page, limit, "-1");

            List<AppMatterT> appMatterTS = qaUtils.qaList(0, page, limit, "-1");
            arrayList.addAll(appEssayTS);
            arrayList.addAll(appMatterTS);
        }
        return arrayList;
    }


    public String dataInfo(List list) {
        String info = "";
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getClass());
            System.out.println(list.get(i).getClass() == AppMatterT.class);
            if (list.get(i).getClass() == AppEssayT.class) { //咨询
                AppEssayT essayt = (AppEssayT) list.get(i);
                if (essayt != null) {
                    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
                    String format = sdf.format(essayt.getGroundTime());


                    info += " <div class=\"list-item\">\n" +
                            "                                <!-- 资讯 -->\n" +
                            "                                <div class=\"list-item-content\">\n" +
                            "                                    <div class=\"list-item-content-left\">\n" +
                            "                                        <a target=\"_blank\" href=\"/consult_solo/" + essayt.getEssayId() + "\">\n" +
                            "                                            <img src=\"" + essayt.getImgPath() + "\" alt=\"有贝口腔网\">\n" +
                            "                                        </a>\n" +
                            "                                    </div>\n" +
                            "                                    <div class=\"list-item-content-right\">\n" +
                            "                                        <div class=\"list-item-title\">\n" +
                            "                                            <a target=\"_blank\" href=\"/consult_solo/" + essayt.getEssayId() + "\">" + essayt.getEssayTitle() + "</a>\n" +
                            "                                        </div>\n" +
                            "                                        <div class=\"list-item-content-text\">\n" +
                            "                                            <a target=\"_blank\" href=\"/consult_solo/" + essayt.getEssayId() + "\"\n" +
                            "                                              >" + essayt.getEssaySynopsis() + "</a>\n" +
                            "                                        </div>\n" +
                            "                                        <div class=\"list-item-jilu\">\n" +
                            "                                            <div class=\"list-item-infoma-left\">\n" +
                            "                                                <a target=\"_blank\" href=\"/zx/" + essayt.getClassifyId() + "\"\n" +
                            "                                                   class=\"list-item-type\"\n" +
                            "                                                   >" + essayt.getClassifyT().getClassifyName() + "</a>\n" +
                            "\n" +
                            "                                                <span class=\"list-item-time\"\n" +
                            "                                                      >" + format + "</span>\n" +
                            "                                                <a target=\"_blank\" href=\"/consult_solo/" + essayt.getEssayId() + "\">\n" +
                            "                                                    <img src=\"/icon/icon_Browse.png\" alt=\"有贝口腔网\">\n" +
                            "                                                    <span class=\"liulan-text\"><span>" + essayt.getReadCount() + "</span>阅读</span>\n" +
                            "                                                </a>\n" +
                            "                                                <a target=\"_blank\" href=\"/consult_solo/" + essayt.getEssayId() + "\" class=\"item-pinglun-img\">\n" +
                            "                                                    <img src=\"/icon/icon_News.png\" alt=\"有贝口腔网\">\n" +
                            "                                                    <span class=\"liulan-text\"><span>" + essayt.getCommentCount() + "</span>评论</span>\n" +
                            "                                                </a>\n" +
                            "                                            </div>\n" +
                            "\n" +
                            "                                        </div>\n" +
                            "                                    </div>\n" +
                            "                                </div>\n" +
                            "                            </div>";
                }

            } else if (list.get(i).getClass() == AppMatterT.class) {//问答
                System.out.println(list.get(i));

                AppMatterT matter = (AppMatterT) list.get(i);
                SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
                String format = sdf.format(matter.getGroundTime());

                if (matter != null) {
                    info += "<div class=\"list-item\">\n" +
                            "\t<!-- 是问题 -->\n" +
                            "\n" +
                            "\t<div class=\"list-item-title\">\n" +
                            "\t\t<a target=\"_blank\" href=\"/answers_ptl/" + matter.getMatterId() + "\" >" + matter.getMatterTitle() + "</a>\n" +
                            "\t</div>";
                    if (matter.getAnswerCount() > 0) {
                        info += "<div class=\"list-item-content-index\">\n" +
                                "\t\t<div class=\"text-ansower-user\">\n" +
                                "\t\t\t<a target=\"_blank\" href=\"/answers_ptl/" + matter.getMatterId() + "\" class=\"list-item-infoma-user\">\n" +
                                "\t\t\t\t<img src=\"" + matter.getReplyUser().getImgPath() + "\">\n" +
                                "\t\t\t\t<span class=\"user-name\">" + matter.getReplyUser().getUserNickname() + "</span>\n" +
                                "\t\t\t</a>\n" +
                                "\t\t\t<p class=\"text-ansower-p\">\n" +
                                "\t\t\t\t<!-- <span class=\"text-ansower-line\"></span> -->\n" +
                                "\t\t\t\t<span class=\"text-ansower-huida\">[回答内容]</span>\n" +
                                "\t\t\t</p>\n" +
                                "\t\t</div>\n" +
                                "\t\t<p class=\"text-ansower-content\">\n" +
                                "\t\t\t<a target=\"_blank\" href=\"/answers_ptl/" + matter.getMatterId() + "\" >\n" +
                                "\t\t\t" + matter.getAnswerT().getAnswerContent().substring(0, 20) + "..." + "\n" +
                                "\t\t\t</a>\n" +
                                "\t\t</p>\n" +
                                "\t</div>";
                    }
                    info += "<div class=\"list-item-jilu\">\n" +
                            "\t\t<div class=\"list-item-jilu-left\">\n" +
                            "\t\t\t<a target=\"_blank\" href=\"/answers_main/" + matter.getClassifyId() + "\" class=\"list-item-type\"\n" +
                            "\t\t\t   >" + matter.getClassifyT().getClassifyName() + "</a>\n" +
                            "\t\t\t<a target=\"_blank\" href=\"/answers_ptl/" + matter.getMatterId() + "\" class=\"list-item-infoma-user\">\n" +
                            "\t\t\t\t<img src=\"" + matter.getAppUserT().getImgPath() + "\" alt=\"有贝口腔网\">\n" +
                            "\t\t\t\t<span>" + matter.getAppUserT().getUserNickname() + "</span>\n" +
                            "\t\t\t</a>\n" +
                            "\t\t\t<span class=\"list-item-time\"><span\n" +
                            "\t\t\t\t\t>" + format + "</span></span>\n" +
                            "\t\t\t<span class=\"dotted\"></span>\n" +
                            "\t\t\t<a target=\"_blank\" href=\"/answers_ptl/" + matter.getMatterId() + "\"\n" +
                            "\t\t\t   class=\"list-item-huida\"><span>" + matter.getAnswerCount() + "</span>回答</a>\n" +
                            "\t\t\t<span class=\"dotted\"></span>\n" +
                            "\t\t\t<a target=\"_blank\" href=\"/answers_ptl/" + matter.getMatterId() + "\" class=\"list-item-collation\"><span\n" +
                            "\t\t\t\t\t>" + matter.getCollectCount() + "</span>收藏</a>\n" +
                            "\t\t\t<a target=\"_blank\" href=\"/answers_ptl/" + matter.getMatterId() + "\" class=\"item-pinglun-img\">\n" +
                            "\t\t\t\t<img src=\"/img/icon_Browse.png\" alt=\"有贝口腔网\">\n" +
                            "\t\t\t\t<span class=\"liulan-text\"><span>" + matter.getReadCount() + "</span>阅读</span>\n" +
                            "\t\t\t</a>\n" +
                            "\t\t</div>\n" +
                            "\t\t<div class=\"list-item-infoma-right\">\n" +
                            "\t\t\t<a target=\"_blank\" class=\"link-tomore\" href=\"/answers_ptl/" + matter.getMatterId() + "\">邀请你回答</a>\n" +
                            "\t\t</div>\n" +
                            "\t</div></div>";
                }
            }
        }

        return info;
    }
}
